package com.zhengsanming.web.old;

import com.alibaba.fastjson.JSON;
import com.zhengsanming.pojo.Brand;
import com.zhengsanming.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/add")
public class addServlet extends HttpServlet {
    private BrandService service = new BrandService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
//        Brand brand = new Brand();
//        String brandName = req.getParameter("brandName");
//        String companyName = req.getParameter("companyName");
//        String ordered = req.getParameter("ordered");
//        String descript = req.getParameter("descript");
//        String status = req.getParameter("status");
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//        brand.setOrdered(Integer.parseInt(ordered));
//        brand.setDescription(descript);
//        brand.setStatus(Integer.parseInt(status));
//        service.add(brand);
        //跨域
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        //接受数据
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);
        service.add(brand);
        List<Brand> brands = service.selectAll();
        String brand1 = JSON.toJSONString(brands);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(brand1);
        System.out.println(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
